package le.ralph.quwen.cn.java.view.activity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import le.ralph.quwen.cn.R;

public class LoginActivity extends AppCompatActivity {

    private Button btnLogin;
    private View progress;
    private LinearLayout loginInputLayout;
    private LinearLayout loginInputPhone;
    private LinearLayout loginInputPassword;
    private ImageView ivBakck;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initListener();
    }

    private void initView() {
        btnLogin = (Button) findViewById(R.id.btn_login);
        progress = findViewById(R.id.layout_progress);
        loginInputLayout = findViewById(R.id.login_input_layout);
        loginInputPhone = (LinearLayout) findViewById(R.id.login_input_layout_phone);
        loginInputPassword = (LinearLayout) findViewById(R.id.login_input_layout_password);
        ivBakck = findViewById(R.id.iv_login_back);
    }

    private void initListener() {
        btnLogin.setOnClickListener(defaultOnClickListener);
        ivBakck.setOnClickListener(defaultOnClickListener);
    }

    private View.OnClickListener defaultOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.btn_login:
                    onLogin();
                    break;
                case R.id.iv_login_back:
                    onBack();
                    break;
            }
        }
    };

    private void onBack() {
        this.finish();
    }

    private void onLogin() {
        // 计算出控件的高与宽
        float fabWidth = btnLogin.getMeasuredWidth();
        float fabHeight = btnLogin.getMeasuredHeight();
        // 隐藏输入框
        loginInputPhone.setVisibility(View.INVISIBLE);
        loginInputPassword.setVisibility(View.INVISIBLE);
        inputAnimator(loginInputLayout, fabWidth, fabHeight);
    }

    /**
     * 恢复初始状态
     */
    private void recovery() {
        progress.setVisibility(View.GONE);
        loginInputLayout.setVisibility(View.VISIBLE);
        loginInputPhone.setVisibility(View.VISIBLE);
        loginInputPassword.setVisibility(View.VISIBLE);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) loginInputLayout.getLayoutParams();
        params.leftMargin = 0;
        params.rightMargin = 0;
        loginInputLayout.setLayoutParams(params);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(loginInputLayout, "scaleX", 0.5f, 1f);
        animator2.setDuration(500);
        animator2.setInterpolator(new AccelerateDecelerateInterpolator());
        animator2.start();
    }

    /**
     * 输入框的动画效果
     *
     * @param view 控件
     * @param w    宽
     * @param h    高
     */
    private void inputAnimator(final View view, float w, float h) {

        AnimatorSet set = new AnimatorSet();

        ValueAnimator animator = ValueAnimator.ofFloat(0, w);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (Float) animation.getAnimatedValue();
                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view
                        .getLayoutParams();
                params.leftMargin = (int) value;
                params.rightMargin = (int) value;
                view.setLayoutParams(params);
            }
        });

        ObjectAnimator animator2 = ObjectAnimator.ofFloat(loginInputLayout,
                "scaleX", 1f, 0.5f);
        set.setDuration(1000);
        set.setInterpolator(new AccelerateDecelerateInterpolator());
        set.playTogether(animator, animator2);
        set.start();
        set.addListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                /**
                 * 动画结束后，先显示加载的动画，然后再隐藏输入框
                 */
                progress.setVisibility(View.VISIBLE);
                progressAnimator(progress);
                loginInputLayout.setVisibility(View.INVISIBLE);

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }
        });

    }

    /**
     * 出现进度动画
     *
     * @param view
     */
    private void progressAnimator(final View view) {
        PropertyValuesHolder animator = PropertyValuesHolder.ofFloat("scaleX",
                0.5f, 1f);
        PropertyValuesHolder animator2 = PropertyValuesHolder.ofFloat("scaleY",
                0.5f, 1f);
        ObjectAnimator animator3 = ObjectAnimator.ofPropertyValuesHolder(view,
                animator, animator2);
        animator3.setDuration(1000);
        animator3.setInterpolator(new JellyInterpolator());
        animator3.start();

    }

    public class JellyInterpolator extends LinearInterpolator {
        private float factor;

        public JellyInterpolator() {
            this.factor = 0.15f;
        }

        @Override
        public float getInterpolation(float input) {
            return (float) (Math.pow(2, -10 * input)
                    * Math.sin((input - factor / 4) * (2 * Math.PI) / factor) + 1);
        }
    }
}
